package com.jyxd.web.controller.ZNurseRecordUpdateLog;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jyxd.web.data.ZNurseRecordUpdateLog.ZNurseRecordUpdateLogEntity;
import com.jyxd.web.service.ZNurseRecordUpdateLog.ZNurseRecordUpdateLogService;
import com.jyxd.web.util.HttpCode;
import net.sf.json.JSONObject;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping(value = "/nurseRecordUpdateLog")
public class ZNurseRecordUpdateLogController {

    @Resource
    private ZNurseRecordUpdateLogService nurseRecordUpdateLogService;

    /**
     * 查询最新修改信息
     */
    @GetMapping(value = "/queryOne/{nursingRecordId}")
    public String queryOne(@PathVariable("nursingRecordId") String nursingRecordId) {
        JSONObject json = new JSONObject();
        json.put("code", HttpCode.FAILURE_CODE.getCode());
        json.put("data", new ArrayList<>());
        json.put("msg", "暂无数据");
        LambdaQueryWrapper<ZNurseRecordUpdateLogEntity> lqw = new LambdaQueryWrapper<ZNurseRecordUpdateLogEntity>();
        lqw.eq(ZNurseRecordUpdateLogEntity::getNurId, nursingRecordId)
                .orderByDesc(ZNurseRecordUpdateLogEntity::getUpdateTime)
                .last("LIMIT 1");
        ZNurseRecordUpdateLogEntity latestRecord = nurseRecordUpdateLogService.getOne(lqw);;
        json.put("code", HttpCode.OK_CODE.getCode());
        json.put("data", latestRecord);
        return json.toString();
    }

    /**
     * 查询详细记录
     */
    @GetMapping(value = "/queryDetail/{nursingRecordId}")
    public String queryDetail(@PathVariable("nursingRecordId") String nursingRecordId) {
        JSONObject json = new JSONObject();
        json.put("code", HttpCode.FAILURE_CODE.getCode());
        json.put("data", new ArrayList<>());
        json.put("msg", "暂无数据");
        LambdaQueryWrapper<ZNurseRecordUpdateLogEntity> lqw = new LambdaQueryWrapper<ZNurseRecordUpdateLogEntity>();
        lqw.eq(ZNurseRecordUpdateLogEntity::getNurId, nursingRecordId)
                .orderByDesc(ZNurseRecordUpdateLogEntity::getUpdateTime);
        List<ZNurseRecordUpdateLogEntity> list = nurseRecordUpdateLogService.list(lqw);
        json.put("code", HttpCode.OK_CODE.getCode());
        json.put("data", list);
        return json.toString();
    }
}
